package com.xbongbong.workflow.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.workflow.domain.dao.WorkflowNodeDao;
import com.xbongbong.workflow.domain.entity.WorkflowNodeEntity;
import com.xbongbong.workflow.domain.entity.ext.WorkflowNodeEntityExt;
import com.xbongbong.workflow.model.WorkflowNodeModel;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 */
@Service("workflowNodeModel")
public class WorkflowNodeModelImpl implements WorkflowNodeModel {
    @Resource
    private WorkflowNodeDao workflowNodeDao;

    @Override
    public Integer insert(WorkflowNodeEntity entity) {
        long now = DateTimeUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        entity.setDel(DelEnum.NORMAL.getDel());
        return workflowNodeDao.insert(entity);
    }

    @Override
    public void insertBatch(List<WorkflowNodeEntity> list) {
        workflowNodeDao.insertBatch(list);
    }

    @Override
    public void insertExtBatch(List<WorkflowNodeEntityExt> list) {
        if (CollectionsUtil.isEmpty(list)) {
            return;
        }
        workflowNodeDao.insertExtBatch(list);
    }



    @Override
    public List<WorkflowNodeEntity> findEntitys(Map<String, Object> param) {
        return workflowNodeDao.findEntities(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return workflowNodeDao.getEntitiesCount(param);
    }

    @Override
    public Integer update(WorkflowNodeEntity entity) {
        long now = DateTimeUtil.getInt();
        entity.setUpdateTime(now);
        return workflowNodeDao.update(entity);
    }

    @Override
    public Integer deleteByKey(Long key, String corpid) {
        return workflowNodeDao.deleteByKey(key, corpid);
    }

    @Override
    public WorkflowNodeEntity getByKey(Long key, String corpid) {
        return workflowNodeDao.getByKey(key, corpid);
    }
    
    @Override
    public WorkflowNodeEntityExt getExtByKey(Long key, String corpid) {
        return workflowNodeDao.getExtByKey(key, corpid);
    }
    
    @Override
    public List<WorkflowNodeEntity> getByWorkflowIdAndPrevId(Long workflowId, String prevId, String corpid) {
        return workflowNodeDao.getByWorkflowIdAndPrevId( workflowId,  prevId,  corpid);
    }

    @Override
    public Integer deleteBatch(Collection<Long> idIn, String corpid) {
        return workflowNodeDao.deleteBatch(idIn, corpid);
    }

    @Override
    public Integer deleteByWorkflowId(List<Long> workflowIdList, String corpid) {
        return workflowNodeDao.deleteByWorkflowId(workflowIdList, corpid);
    }

    @Override
    public WorkflowNodeEntity getByUId(Long workflowId,String corpid, String uId) {
        return workflowNodeDao.getByUId(workflowId, uId, corpid);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public List<WorkflowNodeEntity> getWorkflowNodeList(String corpid, Long workflowId, String orderByStr) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(StringConstant.CORPID, corpid);
        param.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
        param.put("workflowId", workflowId);
        param.put("orderByStr", orderByStr);
        List<WorkflowNodeEntity> workflowNodeList = workflowNodeDao.findEntities(param);
        return workflowNodeList;
    }

    @Override
    public List<WorkflowNodeEntity> getWorkflowNodeListIgnoreDel(String corpid, Long workflowId) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(StringConstant.CORPID, corpid);
        param.put("workflowId", workflowId);
        List<WorkflowNodeEntity> workflowNodeList = workflowNodeDao.findEntities(param);
        return workflowNodeList;
    }

    @Override
    public List<WorkflowNodeEntityExt> list(Map<String, Object> param) {
        return workflowNodeDao.list(param);
    }

    @Override
    public Integer updateWrapperId(List<WorkflowNodeEntity> list, String corpid) {
        return workflowNodeDao.updateWrapperId(list, corpid);
    }

    @Override
    public Integer updateWrapperIdByIdIn(List<Long> list, String wrapperId, String corpid) {
        return workflowNodeDao.updateWrapperIdByIdIn(list, wrapperId, corpid);
    }

    @Override
    public List<WorkflowNodeEntity> getByIdIn(Collection<Long> idIn, String corpid) {
        if (CollectionUtils.isEmpty(idIn)) {
            return new ArrayList<>();
        }
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(StringConstant.CORPID, corpid);
        param.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
        param.put("idIn", idIn);
        return findEntitys(param);
    }

    @Override
    public Integer sort(List<WorkflowNodeEntity> nodeList, String corpid) {
        return workflowNodeDao.sort(nodeList, corpid);
    }

    @Override
    public List<WorkflowNodeEntity> getByWrapperId(String wrapperId, String corpid, Long workflowId) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(StringConstant.CORPID, corpid);
        param.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
        param.put("workflowId", workflowId);
        param.put("wrapperId", wrapperId);
        return findEntitys(param);
    }

    @Override
    public void updateConfigData(List<? extends WorkflowNodeEntity> list, String corpid) {
        workflowNodeDao.updateConfigData(list, corpid);
    }

    @Override
    public Integer updatePrevIdBatch(List<WorkflowNodeEntity> updateList, String corpid) {
        if (CollectionsUtil.isEmpty(updateList)) {
            return 0;
        }
        return workflowNodeDao.updatePrevIdBatch(updateList, corpid);
    }
}
